package com.example.demo.controller;

import com.example.demo.entity.Skus;
import com.example.demo.service.SkusService;
import com.example.demo.controller.vo.ResponseResult;
import com.example.demo.controller.vo.BatchDeleteSkusRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import lombok.extern.slf4j.Slf4j;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/admin/skus")
public class SkusController {

    @Autowired
    private SkusService skusService;

    @PostMapping
    public ResponseResult<?> createSkus(@RequestHeader("token") String token,
                                      @RequestParam Integer status,
                                      @RequestParam String name,
                                      @RequestParam Integer order,
                                      @RequestParam String defaults) {
        log.info("Creating new skus: name={}, status={}, order={}", name, status, order);
        Skus skus = new Skus();
        skus.setStatus(status);
        skus.setName(name);
        skus.setOrder(order);
        skus.setDefaults(defaults);
        Skus createdSkus = skusService.createSkus(skus);
        log.info("Skus created successfully: id={}", createdSkus.getId());
        return ResponseResult.success(createdSkus);
    }

    @PostMapping("/{id}")
    public ResponseResult<?> updateSkus(@RequestHeader("token") String token,
                                      @PathVariable Integer id,
                                      @RequestParam Integer status,
                                      @RequestParam String name,
                                      @RequestParam Integer order,
                                      @RequestParam String defaults) {
        log.info("Updating skus: id={}, name={}, status={}, order={}", id, name, status, order);
        Skus skus = new Skus();
        skus.setId(id);
        skus.setStatus(status);
        skus.setName(name);
        skus.setOrder(order);
        skus.setDefaults(defaults);
        Skus updatedSkus = skusService.updateSkus(skus);
        log.info("Skus updated successfully: id={}", updatedSkus.getId());
        return ResponseResult.success(true);
    }

    @PostMapping("/{id}/update_status")
    public ResponseResult<?> updateSkusStatus(@RequestHeader("token") String token,
                                           @PathVariable Integer id,
                                           @RequestParam Integer status) {
        log.info("Updating skus status: id={}, status={}", id, status);
        boolean result = skusService.updateSkusStatus(id, status);
        if (result) {
            log.info("Skus status updated successfully: id={}", id);
        } else {
            log.warn("Failed to update skus status: id={}", id);
        }
        return ResponseResult.success(result);
    }

    @GetMapping("/{page}")
    public ResponseResult<?> getSkusList(@RequestHeader("token") String token,
                                       @PathVariable Integer page,
                                       @RequestParam(defaultValue = "10") Integer limit,
                                       @RequestParam(required = false) String keyword) {
        log.info("Fetching skus list: page={}, limit={}, keyword={}", page, limit, keyword);
        Object result = skusService.getSkusList(page, limit, keyword);
        log.debug("Fetched {} skus", result instanceof Map ? ((Map<?, ?>) result).get("totalCount") : "unknown");
        return ResponseResult.success(result);
    }

    @PostMapping("/delete_all")
    public ResponseResult<?> batchDeleteSkus(@RequestHeader("token") String token,
                                           @RequestBody BatchDeleteSkusRequest request) {
        log.info("Batch deleting skus: ids={}", request.getIds());
        boolean result = skusService.batchDeleteSkus(request.getIds());
        if (result) {
            log.info("Skus deleted successfully: ids={}", request.getIds());
        } else {
            log.warn("Failed to delete skus: ids={}", request.getIds());
        }
        return ResponseResult.success(result);
    }
}
